package com.choosefine.it.team.service.Impl;

import java.util.Map;

import com.choosefine.it.basedata.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.manager.model.ManagerPublishFindTeamInfo;
import com.choosefine.it.team.dao.TeamProjectCollectionMapper;
import com.choosefine.it.team.model.vo.TeamProjectCollectionVO;
import com.choosefine.it.team.service.TeamProjectCollectionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("teamProjectCollectionService")
public class TeamProjectCollectionServiceImpl implements TeamProjectCollectionService {
	@Autowired
	private TeamProjectCollectionMapper teamProjectCollectionMapper;
	@Autowired
	MongoTemplate mongoTemplate;
	@Autowired
	CountService countService;

	@Override
	public Page<TeamProjectCollectionVO> getTeamProjectCollectionList(Map<String, Object> params,PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<TeamProjectCollectionVO> list = teamProjectCollectionMapper.getTeamProjectCollectionList(params);
		return list;
	}

	@Override/*取消收藏3.1yuxin*/
	public int deleteProjectCollect(Map<String, Object> map) {
		int num=0;
		int count= countService.isInsert(map);
		if(count>0){
			num=teamProjectCollectionMapper.cancleProjectCollect(map);
			int favorite=countService.favoriteCount(map);
			Update update=new Update();
			update.set("favoriteCount",favorite);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(map.get("publishId"))),update, ManagerPublishFindTeamInfo.class);
		}
		return num;
	}

//	@Override
//	public Page<TeamProjectCollectionVO> getPublishInfoByIdes(List<Integer> ides,PageDTO pageDTO) {
//		PageHelper.startPage(PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize()));
//		Page<TeamProjectCollectionVO> list= new Page<>();
//		mongoTemplate.find(Query.query(Criteria.where("_id").in(ides)),TeamProjectCollectionVO.class);
//		return list;
//	}

	@Override
	public int insertSelective(Map<String, Object> map) {
		int num=0;
		int count= countService.isInsert(map);//查询是否已收藏
		if(count==0){
			num=teamProjectCollectionMapper.insert(map);
			int favorite=countService.favoriteCount(map);
			Update update=new Update();
			update.set("favoriteCount",favorite);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(map.get("publishId"))),update, ManagerPublishFindTeamInfo.class);
		}
		return num;
	}


}
